home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / program / gcc / ixemul-4.lha / ixemul-41.4 / network / res_init.c < prev    next >
C/C++ Source or Header  |  1995-05-18  |  6KB  |  193 lines

  1. /*-
  2.  * Copyright (c) 1985, 1989 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)res_init.c    6.15 (Berkeley) 2/24/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/param.h>
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <arpa/nameser.h>
  43. #include <resolv.h>
  44. #include <unistd.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48.  
  49. /*
  50.  * Resolver state default settings
  51.  */
  52.  
  53. struct state _res = {
  54.     RES_TIMEOUT,                   /* retransmition time interval */
  55.     4,                             /* number of times to retransmit */
  56.     RES_DEFAULT,            /* options flags */
  57.     1,                             /* number of name servers */
  58. };
  59.  
  60. /*
  61.  * Set up default settings.  If the configuration file exist, the values
  62.  * there will have precedence.  Otherwise, the server address is set to
  63.  * INADDR_ANY and the default domain name comes from the gethostname().
  64.  *
  65.  * The configuration file should only be used if you want to redefine your
  66.  * domain or run without a server on your machine.
  67.  *
  68.  * Return 0 if completes successfully, -1 on error
  69.  */
  70. res_init()
  71. {
  72.     register FILE *fp;
  73.     register char *cp, **pp;
  74.     register int n;
  75.     char buf[BUFSIZ];
  76.     int nserv = 0;    /* number of nameserver records read from file */
  77.     int haveenv = 0;
  78.     int havesearch = 0;
  79.  
  80.     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
  81.     _res.nsaddr.sin_family = AF_INET;
  82.     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
  83.     _res.nscount = 1;
  84.  
  85.     /* Allow user to override the local domain definition */
  86.     if ((cp = getenv("LOCALDOMAIN")) != NULL) {
  87.         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
  88.         haveenv++;
  89.     }
  90.  
  91.     if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
  92.         /* read the config file */
  93.         while (fgets(buf, sizeof(buf), fp) != NULL) {
  94.         /* read default domain name */
  95.         if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
  96.             if (haveenv)    /* skip if have from environ */
  97.                 continue;
  98.             cp = buf + sizeof("domain") - 1;
  99.             while (*cp == ' ' || *cp == '\t')
  100.                 cp++;
  101.             if ((*cp == '\0') || (*cp == '\n'))
  102.                 continue;
  103.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  104.             if ((cp = index(_res.defdname, '\n')) != NULL)
  105.                 *cp = '\0';
  106.             havesearch = 0;
  107.             continue;
  108.         }
  109.         /* set search list */
  110.         if (!strncmp(buf, "search", sizeof("search") - 1)) {
  111.             if (haveenv)    /* skip if have from environ */
  112.                 continue;
  113.             cp = buf + sizeof("search") - 1;
  114.             while (*cp == ' ' || *cp == '\t')
  115.                 cp++;
  116.             if ((*cp == '\0') || (*cp == '\n'))
  117.                 continue;
  118.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  119.             if ((cp = index(_res.defdname, '\n')) != NULL)
  120.                 *cp = '\0';
  121.             /*
  122.              * Set search list to be blank-separated strings
  123.              * on rest of line.
  124.              */
  125.             cp = _res.defdname;
  126.             pp = _res.dnsrch;
  127.             *pp++ = cp;
  128.             for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
  129.                 if (*cp == ' ' || *cp == '\t') {
  130.                     *cp = 0;
  131.                     n = 1;
  132.                 } else if (n) {
  133.                     *pp++ = cp;
  134.                     n = 0;
  135.                 }
  136.             }
  137.             /* null terminate last domain if there are excess */
  138.             while (*cp != '\0' && *cp != ' ' && *cp != '\t')
  139.                 cp++;
  140.             *cp = '\0';
  141.             *pp++ = 0;
  142.             havesearch = 1;
  143.             continue;
  144.         }
  145.         /* read nameservers to query */
  146.         if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) &&
  147.            nserv < MAXNS) {
  148.             cp = buf + sizeof("nameserver") - 1;
  149.             while (*cp == ' ' || *cp == '\t')
  150.                 cp++;
  151.             if ((*cp == '\0') || (*cp == '\n'))
  152.                 continue;
  153.             if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
  154.             inet_addr(cp)) == (unsigned)-1) {
  155.                 _res.nsaddr_list[nserv].sin_addr.s_addr
  156.                 = INADDR_ANY;
  157.                 continue;
  158.             }
  159.             _res.nsaddr_list[nserv].sin_family = AF_INET;
  160.             _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
  161.             nserv++;
  162.             continue;
  163.         }
  164.         }
  165.         if (nserv > 1) 
  166.         _res.nscount = nserv;
  167.         (void) fclose(fp);
  168.     }
  169.     if (_res.defdname[0] == 0) {
  170.         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
  171.            (cp = index(buf, '.')))
  172.             (void)strcpy(_res.defdname, cp + 1);
  173.     }
  174.  
  175.     /* find components of local domain that might be searched */
  176.     if (havesearch == 0) {
  177.         pp = _res.dnsrch;
  178.         *pp++ = _res.defdname;
  179.         for (cp = _res.defdname, n = 0; *cp; cp++)
  180.             if (*cp == '.')
  181.                 n++;
  182.         cp = _res.defdname;
  183.         for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH;
  184.             n--) {
  185.             cp = index(cp, '.');
  186.             *pp++ = ++cp;
  187.         }
  188.         *pp++ = 0;
  189.     }
  190.     _res.options |= RES_INIT;
  191.     return (0);
  192. }
  193.